package com.machine.filling.Test.ad;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.machine.filling.R;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.http.response.ADListResBean;
import com.machine.filling.ui.view.NoPreloadViewPager;
import com.machine.filling.util.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/6/5 0005.
 */
@Deprecated
public class TestAdFragment extends Fragment {
    private static final int UPTATE_VIEWPAGER = 0;
    private static final String TAG = "TestAdFragment";
    private NoPreloadViewPager mViewPager;
    private String respMsg;
    private Activity mActivity;
    private List<View> imgList = new ArrayList<View>();

    private int autoCurrIndex = 0;//设置当前 第几个图片 被选中
    private long period = 5000;//轮播图展示时长,默认5秒
    private List<ADListResBean.AdListBean> list;
    private AdPagerAdapter adPagerAdapter2;
    private List<ADListResBean.AdListBean> adList2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fg_ad, null);
        mViewPager = view.findViewById(R.id.viewpager);
        mActivity = getActivity();
        initData();
        return view;
    }

    private void initData() {
        bt_get_ad_list();

    }

    public void downLoadFiles(final String downloadUrl, final String fileName) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int xx = FileUtils.downloadFiles(downloadUrl, "XDKJ" + File.separator + "ad", downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1));
                Log.d(TAG, "下载结果=========: " + xx);
            }
        }).start();
    }


    /**
     * 获取广告列表
     */
    private void bt_get_ad_list() {

        APIHttpManager.getInstance().getAdList(TestCommon.token, TestCommon.machine_id,
                new BaseHttpStringCallback() {
                    @Override
                    public void onSuccess(Call call, String result) {
                        Log.d(TAG, "onSuccess() returned: " + result);
                        ADListResBean resBean = new Gson().fromJson(result, ADListResBean.class);
//                        showADList(resBean);
                        showAdListUI(resBean);
                    }

                    @Override
                    public void onFailed(String errorCode, String errorMsg, Exception exception) {
                        Log.d(TAG, "onSuccess() returned: " + errorMsg);
                        ADListResBean nullBean = new ADListResBean();
                        ArrayList<ADListResBean.AdListBean> list = new ArrayList<>();

                        ADListResBean.AdListBean listBean = new ADListResBean.AdListBean();
                        listBean.setImgPath(Environment.getExternalStorageDirectory()
                                .getAbsolutePath()
                                + File.separator
                                + File.separator
                                + "XDKJ" + File.separator + "ad" + File.separator + "1.mp4");
                        listBean.setType(2);
                        listBean.setTimeLong(5);
                        list.add(listBean);

                        ADListResBean.AdListBean listBean1 = new ADListResBean.AdListBean();
                        listBean1.setImgPath(Environment.getExternalStorageDirectory()
                                .getAbsolutePath()
                                + File.separator
                                + File.separator
                                + "XDKJ" + File.separator + "ad" + File.separator + "6564748d67f911e882bfa3d10bc0a4b5.jpg");
                        listBean1.setType(1);
                        listBean1.setTimeLong(0);
                        list.add(listBean1);

                        ADListResBean.AdListBean listBean2 = new ADListResBean.AdListBean();
                        listBean2.setImgPath(Environment.getExternalStorageDirectory()
                                .getAbsolutePath()
                                + File.separator
                                + File.separator
                                + "XDKJ" + File.separator + "ad" + File.separator + "d66650a267f811e8b4ab0b143b72c21f.jpg");
                        listBean2.setType(1);
                        listBean2.setTimeLong(0);
                        list.add(listBean2);

                        nullBean.setAdList(list);

                        showAdListUI(nullBean);
                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }

    //    -----
    private Handler handler = new Handler() {

        @SuppressLint("HandlerLeak")
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {

                case UPTATE_VIEWPAGER:
                    if (msg.arg1 != 0) {
                        mViewPager.setCurrentItem(msg.arg1);
                    } else {
                        //false 当从末页调到首页时，不显示翻页动画效果，
                        mViewPager.setCurrentItem(msg.arg1, false);
                    }
                    break;
                default:
                    break;
            }
        }
    };


    /**
     * 展示广告信息数据 (此处网络图片不取,只从下载地址取广告轮播)
     *
     * @param resBean
     */
    public void showAdListUI(ADListResBean resBean) {
        handler.removeCallbacks(lubotu);
        adList2 = resBean.getAdList();

        autoBanner2(adList2);
//
//        adPagerAdapter2 = new AdPagerAdapter(mActivity, adList2);
//        mViewPager.setAdapter(adPagerAdapter2);
    }

    private void autoBanner2(final List<ADListResBean.AdListBean> list) {
        mViewPager.setOffscreenPageLimit(0);
        adPagerAdapter2 = new AdPagerAdapter(mActivity, list);
        mViewPager.setAdapter(adPagerAdapter2);

        mViewPager.setOnPageChangeListener(new NoPreloadViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                handler.removeCallbacks(lubotu);
                autoCurrIndex = position;//动态设定轮播图每一页的停留时间
                handler.postDelayed(lubotu, (list.get(position).getTimeLong() == 0
                        ? 5 : list.get(position).getTimeLong()) * 1000);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        handler.postDelayed(lubotu, (list.get(0).getTimeLong() == 0
                ? 5 : list.get(0).getTimeLong()) * 1000);
    }

    public Runnable lubotu = new Runnable() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = UPTATE_VIEWPAGER;
            if (autoCurrIndex == adList2.size() - 1) {
                autoCurrIndex = -1;
            }
            message.arg1 = autoCurrIndex + 1;
            handler.sendMessage(message);
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        handler.removeCallbacks(lubotu);
    }
}

